<?php
    class Admincp_SalesController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->view->headTitle('Quản lý ưu đãi');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Sales();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            if($_POST['add']) {
                $this->_redirect('admincp/sales/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiSales($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listSales());
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction()
        {
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            
            if($this->_request->isPost()) {
                $description['vi'] = trim(stripslashes($this->_arrParam['description-vi']));
                $description['zh'] = trim(stripslashes($this->_arrParam['description-zh']));
                $description['en'] = trim(stripslashes($this->_arrParam['description-en']));
                $description['trv'] = trim(stripslashes($this->_arrParam['description-trv']));
                $type = (int) $this->_arrParam['type_hotel'];
                $type_id = (int) $this->_arrParam['type_id'];
                $sales = (int) $this->_arrParam['sales'];
                $start_date = $this->_arrParam['start_date'];
                $finish_date = $this->_arrParam['finish_date'];
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $common = new Admincp_Model_Common();
                $data_sales = array(
                    'type' => $type,
                    'type_id' => $type_id,
                    'sales' => $sales,
                    'start_date' => $common->formatDateToSql($start_date),
                    'finish_date' => $common->formatDateToSql($finish_date),
                    'status' => 2,
                );
                $sales_id = $this->_model->addSales($data_sales);
                foreach($description as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'sales_id' => $sales_id,
                                'language_id' => $lang['id'],
                                'description' => $description[$key],
                            );
                            $this->_model->addSalesLanguage($data);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/sales');
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $this->view->data = $this->_model->getSalesLanguageById($id);
            $this->view->sales = $this->_model->getSalesById($id);
            
            if($this->_request->isPost()) {
                $description['vi'] = trim(stripslashes($this->_arrParam['description-vi']));
                $description['zh'] = trim(stripslashes($this->_arrParam['description-zh']));
                $description['en'] = trim(stripslashes($this->_arrParam['description-en']));
                $description['trv'] = trim(stripslashes($this->_arrParam['description-trv']));
                $type = (int) $this->_arrParam['type_hotel'];
                $type_id = (int) $this->_arrParam['type_id'];
                $sales = (int) $this->_arrParam['sales'];
                $start_date = $this->_arrParam['start_date'];
                $finish_date = $this->_arrParam['finish_date'];
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $common = new Admincp_Model_Common();
                $data_sales = array(
                    'type' => $type,
                    'type_id' => $type_id,
                    'sales' => $sales,
                    'start_date' => $common->formatDateToSql($start_date),
                    'finish_date' => $common->formatDateToSql($finish_date),
                );
                $sales_id = $this->_model->updateSales($data_sales, $id);
                foreach($description as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'language_id' => $lang['id'],
                                'description' => $description[$key],
                            );
                            $this->_model->updateSalesLanguage($data, $id);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/sales');
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deleteSales($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'status'=>$status,
            );
            $this->_model->updateSales($data, $id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function searchAction()
        {
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
		    $sales = strtolower(trim(strip_tags($this->_arrParam['sales'])));
            $type = (int) $this->_arrParam['type'];
		    $status = (int) $this->_arrParam['status'];
		    $this->view->keyword = array(
	            'sales'=>$sales,
                'type'=>$type,
	            'status'=>$status,
		    );
		    $data = array(
	            'sales'=> '%'.str_replace(' ','%',$strFlex->filter($sales)).'%',
                'type'=>$type,
	            'status'=>$status,
		    );
		    $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchSales($data));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(40);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
		    $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function loadhotelAction()
        {
            $this->_helper->layout()->disableLayout();
            $type_hotel = (int) $this->_arrParam['type_hotel'];
            $listHotel = $this->_model->listHotelByType($type_hotel);
            if(!empty($type_hotel)) {
                foreach($listHotel as $value) {
                    if($value['id'] == $type_hotel) {
                        echo '<option selected="selected" value="'.$value['id'].'">'.$value['name'].'</option>';
                    }
                    else {
                        echo '<option value="'.$value['id'].'">'.$value['name'].'</option>';
                    }
                }
            }
            else {
                foreach($listHotel as $value) {
                    echo '<option value="'.$value['id'].'">'.$value['name'].'</option>';
                }
            }
        }
    }
